package cn.newtouch.system.controller;

import cn.newtouch.basic.annotation.PreAuthorize;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;

import cn.newtouch.system.domain.Role;
import cn.newtouch.system.query.RoleQuery;
import cn.newtouch.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色的API",description="角色相关的CRUD功能")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有角色" )
    @PreAuthorize(name = "所有角色",sn = "role:getAll")
    public List<Role> findAll(){
        return roleService.loadAll();
    }
    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "通过ID查询角色",sn = "role:get")
    public Role findOne(@PathVariable Long id){
       return roleService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "通过ID删除角色",sn = "role:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            roleService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    @PreAuthorize(name = "保存角色",sn = "role:save")
    public JsonResult updateOrAdd(@RequestBody Role role){
        try {
            if(role.getId()==null){
                //添加操作
                roleService.save(role);
            }else{
                //修改操作
                roleService.update(role);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "分页查询角色",sn = "role:list")
    public PageList<Role> queryPage(@RequestBody RoleQuery query){
        try {
            return roleService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "批量删除角色",sn = "role:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            roleService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

}
